package com.example.demo.controller;

import com.example.demo.controller.dto.AnswerDTO;
import com.example.demo.controller.dto.CommitQuestionnaireDTO;
import com.example.demo.controller.dto.QuestionDTO;
import com.example.demo.controller.dto.QuestionnaireDTO;
import com.example.demo.dao.QuestionnaireDao;
import com.example.demo.entity.AnswerList;
import com.example.demo.entity.Question;
import com.example.demo.utils.Result;
import com.example.demo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: WinstonDeng
 * @Description:
 * @Date: Created in 10:32 2020/4/9
 * @Modified by:
 */
@RestController
@RequestMapping(value = "/questionnaire", produces = "application/json;charset=UTF-8")
public class QuestionnaireController {
    @Autowired
    private QuestionnaireDao questionnaireDao;

    /***
     * 查询未完成的问卷
     * @param questionnaireId
     * @return
     * @throws Exception
     */
    @GetMapping("/{questionnaireId}")
    @ResponseBody
    public ResponseEntity<Result> getById(@PathVariable("questionnaireId") long questionnaireId) throws Exception{
        return ResponseEntity.ok(ResultUtil.success(questionnaireDao.getById(questionnaireId)));
    }

    /***
     * 查询问卷填写记录
     * @return
     * @throws Exception
     */
    @GetMapping("")
    @ResponseBody
    public ResponseEntity<Result> getByFlowIdAndQuestionnaireId(@RequestParam("flowStepId") long flowStepId) throws Exception{
        return ResponseEntity.ok(ResultUtil.success(questionnaireDao.getQuestionnaireRecordByFlowStepId(flowStepId)));
    }

    /***
     * 提交问卷
     * @return
     * @throws Exception
     */
    @PostMapping("")
    @ResponseBody
    public ResponseEntity<Result> commitQuestionnaire(@RequestBody CommitQuestionnaireDTO commitQuestionnaireDTO) throws Exception{
        long flowStepId = commitQuestionnaireDTO.getFlowStepId();
        List<Long> questionList = commitQuestionnaireDTO.getQustionIdList();
        List<String> answerList = commitQuestionnaireDTO.getAnswerList();
        return ResponseEntity.ok(ResultUtil.success(questionnaireDao.insertQuestionnaireRecord(flowStepId, questionList, answerList)));
    }

    ///
    ///
    /***
     * 查询所有问卷
     * @return
     * @throws Exception
     */
    @GetMapping("/all")
    @ResponseBody
    public ResponseEntity<Result> getAllQuestionnaire() throws Exception{
        return ResponseEntity.ok(ResultUtil.success(questionnaireDao.getAllQuestionnaire()));
    }

    /***
     * 新增问卷
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    @ResponseBody
    public ResponseEntity<Result> addQuestionnaire(@RequestBody QuestionnaireDTO questionnaireDTO) throws Exception{
        return ResponseEntity.ok(ResultUtil.success(questionnaireDao.addQuestionnaire(questionnaireDTO.getDesc())));
    }

    /***
     * 删除问卷
     * @return
     * @throws Exception
     */
    @DeleteMapping("/{questionnaireId}")
    @ResponseBody
    public ResponseEntity<Result> deleteQuestionnaire(@PathVariable("questionnaireId") Long questionnaireId) throws Exception{
        return ResponseEntity.ok(ResultUtil.success(questionnaireDao.deleteQuestionnaire(questionnaireId)));
    }

    /***
     * 修改问卷百分比
     * @return
     * @throws Exception
     */
    @PostMapping("/update/{questionnaireId}")
    @ResponseBody
    public ResponseEntity<Result> updateQuestionnaireScore(@PathVariable("questionnaireId") Long questionnaireId, @RequestParam("basic") Integer basic, @RequestParam("serious") Integer serious) throws Exception{
        return ResponseEntity.ok(ResultUtil.success(questionnaireDao.updateQuestionnaireScore(questionnaireId, basic, serious)));
    }

    /***
     * 查询所有问题
     * @return
     * @throws Exception
     */
    @GetMapping("/{questionnaireId}/all")
    @ResponseBody
    public ResponseEntity<Result> getAllQuestion(@PathVariable("questionnaireId") Long questionnaireId) throws Exception{
        return ResponseEntity.ok(ResultUtil.success(questionnaireDao.getById(questionnaireId)));
    }

    /***
     * 新增问卷问题
     * @return
     * @throws Exception
     */
    @PostMapping("/{questionnaireId}/add")
    @ResponseBody
    public ResponseEntity<Result> addQuestion(@PathVariable("questionnaireId") Long questionnaireId, @RequestBody QuestionDTO questionDTO) throws Exception{
        Question question = new Question();
        question.setQuestionnaireId(questionnaireId);
        question.setOrderNum(questionDTO.getOrderNum());
        question.setDesc(questionDTO.getDesc());
        AnswerList answerList = new AnswerList();
        answerList.setAnswer(new ArrayList<>());
        answerList.setScore(new ArrayList<>());
        question.setAnswerList(answerList);
        return ResponseEntity.ok(ResultUtil.success(questionnaireDao.addQuestion(question)));
    }

    /***
     * 删除问卷问题
     * @return
     * @throws Exception
     */
    @DeleteMapping("/{questionnaireId}/{questionId}")
    @ResponseBody
    public ResponseEntity<Result> deleteQuestion(@PathVariable("questionnaireId") Long questionnaireId, @PathVariable("questionId") Long questionId) throws Exception{
        return ResponseEntity.ok(ResultUtil.success(questionnaireDao.deleteQuestion(questionId)));
    }

    /***
     * 查询所有答案
     * @return
     * @throws Exception
     */
    @GetMapping("/{questionnaireId}/{questionId}/all")
    @ResponseBody
    public ResponseEntity<Result> getAllAnswer(@PathVariable("questionnaireId") Long questionnaireId, @PathVariable("questionId") Long questionId) throws Exception{
        return ResponseEntity.ok(ResultUtil.success(questionnaireDao.getAllAnswer(questionId)));
    }

    /***
     * 新增问题答案
     * @return
     * @throws Exception
     */
    @PostMapping("/{questionnaireId}/{questionId}/add")
    @ResponseBody
    public ResponseEntity<Result> addAnswer(@PathVariable("questionnaireId") Long questionnaireId, @PathVariable("questionId") Long questionId, @RequestBody AnswerDTO answerDTO) throws Exception{
        return ResponseEntity.ok(ResultUtil.success(questionnaireDao.addAnswer(questionId, answerDTO.getAnswer(), answerDTO.getScore())));
    }

    /***
     * 删除问题答案
     * @return
     * @throws Exception
     */
    @DeleteMapping("/{questionnaireId}/{questionId}/deleteAnswer")
    @ResponseBody
    public ResponseEntity<Result> deleteAnswer(@PathVariable("questionnaireId") Long questionnaireId, @PathVariable("questionId") Long questionId, @RequestBody AnswerDTO answerDTO) throws Exception{
        return ResponseEntity.ok(ResultUtil.success(questionnaireDao.deleteAnswer(questionId, answerDTO.getAnswer(), answerDTO.getScore())));
    }
}
